﻿using System;
using System.Net;
using Informant.Authentication;

namespace Informant.ErrorHandling
{
	// FUTURE: This class and subclasses should log errors.
	public class BaseErrorHandler : IErrorHandler
	{
		/// <summary>
		/// Handles the error by taking application specific and external exceptions
		///	then throwing a client friendly exception, ignoring the exception.
		/// </summary>
		/// <param name="exception">The exception.</param>
		/// <exception cref="Informant.Authentication.AuthenticationException"/>
		/// <exception cref="System.Net.WebException"/>
		/// <exception cref="System.InvalidOperationException"/>
		public virtual void MakeExceptionClientFriendly(Exception exception)
		{
			if (exception is WebException)
			{
				if (exception.Message == ErrorResources.WebExceptionForbbidenErrorMessage 
					|| exception.Message == ErrorResources.WebExceptionInternalServerErrorMessage)
				{
					//TODO: Logger.Write("AuthenticationException: " + ErrorResources.AuthenticationExceptionInvalidCredentialsOrTargetServerAddress + Environment.NewLine + exception.StackTrace);
					throw new AuthenticationException(ErrorResources.AuthenticationExceptionInvalidCredentialsOrTargetServerAddress, exception);
				}
				//TODO: Logger.Write("WebException: " + exception.Message + Environment.NewLine + exception.StackTrace);
				throw exception;
			}
			// Used for unknown exception types.
			//TODO: Logger.Write("InvalidOperationException: " + exception.Message + Environment.NewLine + exception.StackTrace);
			throw new InvalidOperationException(exception.Message, exception);
		}
	}
}
